﻿#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.Reflection;
/// <summary>
/// FieldInfo扩展
/// </summary>
public static class ESF_FieldInfoExtend
{
    /// <summary>
    /// 是否有ObsoleteAttribute属性
    /// </summary>
    /// <param name="_property">成员</param>
    /// <returns>true:有,false:无</returns>
    public static bool HasObsoleteAttribute(this FieldInfo _property)
    {
        return _property.GetCustomAttribute<ObsoleteAttribute>() != null;
    }

    /// <summary>
    /// 获得属性CSharp访问关键字
    /// </summary>
    /// <param name="_fieldInfo">FieldInfo</param>
    /// <returns>CSharp关键字</returns>
    public static string GetCSharpAccessKeyword(this FieldInfo _fieldInfo)
    {
        string keyword = string.Empty;
        if (_fieldInfo.FieldType.IsNestedPrivate)
        {
            keyword = enESF_CSharpKeyword.privateKeyword.ESF_FirstAttribute<ESF_AliasTooltipAttribute>().alias;
        }
        else if (_fieldInfo.FieldType.IsPublic)
        {
            keyword = enESF_CSharpKeyword.publicKeyword.ESF_FirstAttribute<ESF_AliasTooltipAttribute>().alias;
        }
        else if (!_fieldInfo.FieldType.IsVisible)
        {
            keyword = enESF_CSharpKeyword.internalKeyword.ESF_FirstAttribute<ESF_AliasTooltipAttribute>().alias;
        }
        else
        {
            keyword = enESF_CSharpKeyword.protectedKeyword.ESF_FirstAttribute<ESF_AliasTooltipAttribute>().alias;
        }
        return keyword;
    }

    /// <summary>
    ///  是否是override
    /// </summary>
    /// <param name="_fieldInfo">属性</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpOverride(this FieldInfo _fieldInfo)
    {
        return _fieldInfo.IsCSharpAbstract() || _fieldInfo.IsCSharpVirtual();
    }

    /// <summary>
    ///  是否是Abstract
    /// </summary>
    /// <param name="_fieldInfo">属性</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpAbstract(this FieldInfo _fieldInfo)
    {
        return _fieldInfo.FieldType.IsAbstract;
    }

    /// <summary>
    ///  是否是Sealed
    /// </summary>
    /// <param name="_fieldInfo">属性</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpSealed(this FieldInfo _fieldInfo)
    {
        return _fieldInfo.FieldType.IsSealed;
    }

    /// <summary>
    ///  是否是Protected
    /// </summary>
    /// <param name="_fieldInfo">属性</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpProtected(this FieldInfo _fieldInfo)
    {
        return !_fieldInfo.FieldType.IsPrimitive && _fieldInfo.FieldType.IsNotPublic;
    }

    /// <summary>
    ///  是否Private
    /// </summary>
    /// <param name="_fieldInfo">属性</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpPrivate(this FieldInfo _fieldInfo)
    {
        return _fieldInfo.FieldType.IsPrimitive;
    }

    /// <summary>
    ///  是否Public
    /// </summary>
    /// <param name="_fieldInfo">属性</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpPublic(this FieldInfo _fieldInfo)
    {
        return _fieldInfo.FieldType.IsPublic;
    }

    /// <summary>
    ///  是否是Virtual
    /// </summary>
    /// <param name="_fieldInfo">属性</param>
    /// <returns>true:是,false:否</returns>
    public static bool IsCSharpVirtual(this FieldInfo _fieldInfo)
    {
        return false;
    }

    /// <summary>
    /// 获得属性CSharp返回参数类型名称
    /// </summary>
    /// <param name="_method">方法</param>
    /// <returns>CSharp关键字</returns>
    public static string GetCSharpReturnParameterTypeName(this FieldInfo _fieldInfo)
    {
        return _fieldInfo.FieldType.GetCSharpTypeCode();
    }

    /// <summary>
    /// 获得Interface属性
    /// </summary>
    /// <param name="_type">类型</param>
    /// <param name="_bindFlags">BindingFlags</param>
    /// <param name="_includeInheritInterface">是否包含继承Interface</param>
    /// <returns>属性</returns>
    public static List<FieldInfo> GetRecursionFields(this Type _type,
        BindingFlags _bindFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
        bool _includeInheritInterface = false)
    {
        List<FieldInfo> result = new List<FieldInfo>();
        FieldInfo[] rst = _type.GetFields(_bindFlags);
        if (rst != null && rst.Length > 0)
        {
            result.AddRange(rst);
        }        
        return result;
    }
}
#endif